package cn.zy.framework;

import cn.zy.framework.bean.Data;
import cn.zy.framework.bean.Handler;
import cn.zy.framework.bean.Param;
import cn.zy.framework.bean.View;
import cn.zy.framework.helper.BeanHelper;
import cn.zy.framework.helper.ConfigHelper;
import cn.zy.framework.helper.ControllerHelper;
import cn.zy.framework.util.*;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Administrator
 * @Description:
 * @Date: Created in 12:23 2021/9/11
 * @Modified By:
 * @version: 1.0
 * 请求转发器，所有请求都由本类处理
 */

@WebServlet(urlPatterns = "/*", loadOnStartup = 0)
public class DispatcherServlet extends HttpServlet {

	@Override
	public void init(ServletConfig config) throws ServletException {
		//初始化相关 Helper 类
		HelperLoader.init();
		//获取 ServletContext 对象（用于注册 Servlet）
		ServletContext servletContext = config.getServletContext();
		//注册处理 JSP 的 Servlet
		ServletRegistration jspServlet = servletContext.getServletRegistration("jsp");
		jspServlet.addMapping(ConfigHelper.getAppJspPath() + "*");

		//注册处理静态资源的默认 Servlet
		ServletRegistration defaultServlet = servletContext.getServletRegistration("default");
		defaultServlet.addMapping(ConfigHelper.getAppAssetPath() + "*");
	}

	@Override
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求方法与请求路径
		String requestMethod = request.getMethod().toLowerCase();
		String requestPah = request.getPathInfo();
		//获取Action 处理器
		Handler handler = ControllerHelper.getHandler(requestMethod, requestPah);
		if (handler != null){
			//获取 Controller 类及其 Bean 实例
			Class<?> controllerClass = handler.getControllerClass();
			Object controllerBean = BeanHelper.getBean(controllerClass);
			//创建请求参数对象
			Map<String, Object> paramMap = new HashMap<String, Object>();
			Enumeration<String> paramNames = request.getParameterNames();
			while (paramNames.hasMoreElements()) {
				String paramName = paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				paramMap.put(paramName, paramValue);
			}
			String body = CodecUtil.decodeURL(StreamUtil.getString(request.getInputStream()));
			if (StringUtil.isNotEmpty(body)) {
				String[] params = StringUtils.split(body, "&");
				if (ArrayUtils.isNotEmpty(params)){
					for (String param : params){
						String[] array = StringUtils.split(param, "=");
						if (ArrayUtils.isNotEmpty(array) && array.length==2){
							String paramName = array[0];
							String paramValue = array[1];
							paramMap.put(paramName, paramValue);
						}
					}
				}
			}
			Param param = new Param(paramMap);
			//调用 Action 方法
			Method actionMethod = handler.getActionMethod();
			Object result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, param);
			//处理 Action 方法返回值
			if (result instanceof View){
				//返回 JSP 页面
				View view = (View) result;
				String path = view.getPath();
				if (StringUtil.isNotEmpty(path)){
					if (path.startsWith("/")){
						response.sendRedirect(request.getContextPath() + path);
					}else {
						Map<String, Object> model = view.getModel();
						for (Map.Entry<String, Object> entry : model.entrySet()){
							request.setAttribute(entry.getKey(), entry.getValue());
						}
						request.getRequestDispatcher(ConfigHelper.getAppJspPath() + path).forward(request, response);
					}
				}
			}else if(request instanceof Data) {
				//返回JSON数据
				Data data = (Data) result;
				Object model = data.getModel();
				if (model != null){
					response.setContentType("application/json");
					response.setCharacterEncoding("UTF-8");
					PrintWriter writer = response.getWriter();
					String json = JsonUtil.toJson(model);
					writer.write(json);
					writer.flush();
					writer.close();
				}
			}



		}







	}
}
